
//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var ismemberVip = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 余额，微信
    payType: "",
    payFlag: true,
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 
    uid: "",
    userType: "",
    // 获取数据
    img1: '',
    img2: '',
    img3: '',
    img4: '',
    saveRMB: '',
    validitydayNum: '',
    canUseAllMa: '',
    mobilePhone: '',
    buttonimg: '',
    membercardFee: '',
    // ismemberVip: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    this.PersonMemberCardData();
  },

  //易易部落折扣全选卡
  PersonMemberCardData: function () {
    var that = this;
    var uid = this.data.uid;
    var img1 = this.data.img1;
    var img2 = this.data.img2;
    var img3 = this.data.img3;
    var img4 = this.data.img4;
    var saveRMB = this.data.saveRMB;
    var validitydayNum = this.data.validitydayNum;
    var canUseAllMa = this.data.canUseAllMa;
    var mobilePhone = this.data.mobilePhone;
    var buttonimg = this.data.buttonimg;
    var ismemberVip = this.data.ismemberVip;
    var membercardFee = this.data.membercardFee;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=PersonMemberCardData&UID=" + uid),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        saveRMB = json.SaveRMB;
        validitydayNum = json.ValidityDayNum;
        canUseAllMa = json.CanUseAllMa;
        img1 = json.Img1;
        img2 = json.Img2;
        img3 = json.Img3;
        img4 = json.Img4;
        mobilePhone = json.MobilePhone;
        buttonimg = json.buttonImg;
        ismemberVip = json.IsMember;
        membercardFee = json.MemberCardFee;
        that.setData({
          saveRMB: saveRMB,
          validitydayNum: validitydayNum,
          canUseAllMa: canUseAllMa,
          mobilePhone: mobilePhone,
          buttonimg: buttonimg,
          img1: img1,
          img2: img2,
          img3: img3,
          img4: img4,
          ismemberVip: ismemberVip,
          membercardFee: membercardFee,
        })
        console.log(json)
      }
    })
  },
  // 手机号获取
  phoneInput: function (e) {
    console.log(e)
    var mobilePhone = this.data.mobilePhone;
    this.setData({
      mobilePhone: e.detail.value,
    })

  },
  // 立即开通会员
  memberShipVip: function () {
    var that = this;
    var uid = this.data.uid;
    var mobilePhone = this.data.mobilePhone;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=PersonBuyMemberCard&UID=" + uid + "&RecommendPhone=" + mobilePhone),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        // console.log(json)
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          // 生成订单   
          var orderData = json.ResultData;
          that.setData({
            payFlag: false,
            orderData: orderData,
          })
          console.log(orderData);
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }

      }
    })
  },
  //去支付
  goPay: function () {
    var that = this;
    var uid = this.data.uid;
    var payType = this.data.payType;
    var orderData = this.data.orderData;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    this.setData({
      payFlag: true,
    })
    if (payType == "1") {
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      // 支付生成的礼包订单
      console.log("method=PersonMemberCardPaySmallProgramOrder&UID=" + uid);
      wx.request({
        // 微信支付
        url: baseJs.api + baseJs.encryUrl("method=PersonMemberCardPaySmallProgramOrder&UID=" + uid),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == 1) {
            wx.requestPayment({
              timeStamp: json.ResultData.timeStamp,
              nonceStr: json.ResultData.nonceStr,
              package: json.ResultData.package,
              signType: json.ResultData.signType,
              paySign: json.ResultData.paySign,
              success(res) {
                console.log(res);
                wx.showToast({
                  title: '支付成功',
                  icon: 'none',
                  duration: 2000
                });
              },
              fail(res) {
                console.log(res);
                wx.showToast({
                  title: res.err_desc,
                  icon: 'none',
                  duration: 2000
                });
              }
            })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    }
  },
  //输入密码--确定 （余额支付）
  surePasswords: function () {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderData = this.data.orderData;
    wx.request({
      // 支付密码是否正确
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            // 余额支付
            url: baseJs.api + baseJs.encryUrl("method=PersonMemberCardPayOrder&UID=" + uid + "&PayType=" + 0),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1000)
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 支付弹出显示
  payShow: function () {
    this.setData({
      payFlag: !this.data.payFlag,
    })

  },
  //余额或微信 
  radioChange: function (e) {
    this.setData({
      payType: e.detail.value,
    });
  },
  //显示支付密码输入层
  showInputLayer: function () {
    this.setData({
      showPayPwdInput: true,
      payFocus: true,
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function () {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: '',
    });
  },
  // 获取焦点
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function (e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
})